﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LOC.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns.Entities.Records
{
    #region Usings

    using System.Diagnostics.Contracts;
    using System.Globalization;
    using System.Text;

    using Rsft.Net.Dns.Interfaces;

    #endregion

    /// <summary>
    ///     The LOC record.
    /// </summary>
    public sealed class LOC : Record
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="LOC"/> class.
        /// </summary>
        /// <param name="recordReader">
        /// The record reader.
        /// </param>
        public LOC(IRecordReader recordReader)
            : base(recordReader)
        {
            Contract.Requires(recordReader != null);

            this.Version = recordReader.ReadByte();
            this.Size = recordReader.ReadByte();
            this.Horizpre = recordReader.ReadByte();
            this.Vertpre = recordReader.ReadByte();
            this.Latitude = recordReader.ReadUInt32();
            this.Longitude = recordReader.ReadUInt32();
            this.Altitude = recordReader.ReadUInt32();
        }

        /// <summary>
        /// </summary>
        public LOC()
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets Altitude.
        /// </summary>
        public uint Altitude { get; private set; }

        /// <summary>
        ///     Gets Horizpre.
        /// </summary>
        public byte Horizpre { get; private set; }

        /// <summary>
        ///     Gets Latitude.
        /// </summary>
        public uint Latitude { get; private set; }

        /// <summary>
        ///     Gets Longitude.
        /// </summary>
        public uint Longitude { get; private set; }

        /// <summary>
        ///     Gets Size.
        /// </summary>
        public byte Size { get; private set; }

        /// <summary>
        ///     Gets Version.
        /// </summary>
        public byte Version { get; private set; }

        /// <summary>
        ///     Gets Vertpre.
        /// </summary>
        public byte Vertpre { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        ///     A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return string.Format(
                CultureInfo.InvariantCulture, 
                "{0} {1} {2} {3} {4} {5}", 
                ToTime(this.Latitude, 'S', 'N'), 
                ToTime(this.Longitude, 'W', 'E'), 
                ToAlt(this.Altitude), 
                SizeToString(this.Size), 
                SizeToString(this.Horizpre), 
                SizeToString(this.Vertpre));
        }

        #endregion

        #region Methods

        /// <summary>
        /// The size to string.
        /// </summary>
        /// <param name="s">
        /// The s.
        /// </param>
        /// <returns>
        /// The size to string.
        /// </returns>
        private static string SizeToString(byte s)
        {
            var strUnit = "cm";
            var intBase = s >> 4;
            var intPow = s & 0x0f;
            if (intPow >= 2)
            {
                intPow -= 2;
                strUnit = "m";
            }

            var sb = new StringBuilder();
            sb.AppendFormat("{0}", intBase);
            for (; intPow > 0; intPow--)
            {
                sb.Append('0');
            }

            sb.Append(strUnit);
            return sb.ToString();
        }

        /// <summary>
        /// The to alt.
        /// </summary>
        /// <param name="a">
        /// The a.
        /// </param>
        /// <returns>
        /// The to alt.
        /// </returns>
        private static string ToAlt(uint a)
        {
            var alt = (a / 100.0) - 100000.00;
            return string.Format(CultureInfo.InvariantCulture, "{0:0.00}m", alt);
        }

        /// <summary>
        /// The to time.
        /// </summary>
        /// <param name="r">
        /// The r.
        /// </param>
        /// <param name="below">
        /// The below.
        /// </param>
        /// <param name="above">
        /// The above.
        /// </param>
        /// <returns>
        /// The to time.
        /// </returns>
        private static string ToTime(uint r, char below, char above)
        {
            const uint Mid = 2147483648; // 2^31
            char dir;
            if (r > Mid)
            {
                dir = above;
                r -= Mid;
            }
            else
            {
                dir = below;
                r = Mid - r;
            }

            var h = r / (360000.0 * 10.0);
            var m = 60.0 * (h - (int)h);
            var s = 60.0 * (m - (int)m);
            return string.Format(CultureInfo.InvariantCulture, "{0} {1} {2:0.000} {3}", (int)h, (int)m, s, dir);
        }

        #endregion
    }
}